package com.gjintel.business.webtypemanage.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gjintel.business.webcontent.model.WebContent;
import com.gjintel.business.webtypemanage.dao.IWebTypeManageDao;
import com.gjintel.business.webtypemanage.model.WebTypeInfo;
import com.gjintel.business.webtypemanage.service.IWebTypeManageService;
import com.gjintel.framework.cache.InitCacheData;
import com.gjintel.framework.cache.InitCacheData.CacheType;
import com.gjintel.framework.dept.model.Dept;
import com.gjintel.framework.util.CommUtil;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.base.service.impl.BaseServiceImpl;
import com.gjintel.sdk.dto.TreeDto;
import com.gjintel.sdk.util.StringUtil;

@Service("webTypeManageService")
public class WebTypeManageServiceImpl extends BaseServiceImpl implements IWebTypeManageService {
	@Autowired
	private IWebTypeManageDao webTypeManageDao;
	
	@Override
	public List<WebTypeInfo> findAll() {
		return this.webTypeManageDao.findAll();
	}
	@Override
	public List<Dept> findDeptAll(){
		return this.webTypeManageDao.findDeptAll();
	}
	@SuppressWarnings("unchecked")
	@Override
	public void findAll(Page page, WebTypeInfo webmanage, Sort sort) {
		this.webTypeManageDao.findAll(page, webmanage, sort);
		WebTypeInfo temp = null;
		Iterator<WebTypeInfo> webmanageIt = page.getResult().iterator();
		WebTypeInfo mdel = null;
		while (webmanageIt.hasNext()) {
			temp = webmanageIt.next();
			if (temp.getPid() != null) {
				mdel = ((WebTypeInfo) (InitCacheData.getInstance().getCacheData(InitCacheData.CacheType.WEB_TYPE).get(temp.getPid())));
				if (mdel != null) {
					temp.setPname(mdel.getName());
				}
			}
		}
	}

	@Override
	public void removeData(String deleteIds) throws Exception {
		WebTypeInfo webmanage = null;
		for (String id : deleteIds.split(",")) {
			webmanage = this.get(WebTypeInfo.class, id);
			if (webmanage != null) {
				webmanage.setFlag(1);
			}
		}
	}

	@Override
	public List<String> getWebManageTree(List<TreeDto> treeList, String id,	String nodeName, String selectIds,boolean isDialog) {
		List<String> selIds = new ArrayList<String>();
		Map<String, List> deptTreeData = InitCacheData.getInstance().getCacheTreeData(InitCacheData.CacheType.WEB_TYPE);
		if (deptTreeData != null) {
			if (StringUtil.isEmpty(id)) {
				id = InitCacheData.ROOT;
			}
			if (StringUtil.isEmpty(nodeName) && StringUtil.isEmpty(selectIds)) {
				List<TreeDto> rootTreeList = this.getTreePoints(deptTreeData, id);
				treeList.addAll(rootTreeList);
			} else {
				// 根据查询信息，查询机构数据，并显示
				List<TreeDto> rootTreeList = this.getTreePoints(deptTreeData, id);
				TreeDto rootTree = null;
				if (rootTreeList != null && rootTreeList.size() == 1) {
					rootTree = rootTreeList.get(0);
					rootTree.setState("open");
					treeList.add(rootTree);
					this.getSelectMenuTree(rootTree, rootTree, nodeName, selectIds, selIds);
				}
			}
		}
		return selIds;
	}
	
	/**
	 * @description 得到节点下的菜单进行回显和查询
	 * @author fengya
	 * @date 2015-7-30 下午05:01:40
	 * @param node
	 * @param rootNode
	 * @param nodeName
	 * @param selectIds
	 * @param selIds
	 * @param isDeptTree
	 * @return void
	 */
	private void getSelectMenuTree(TreeDto node, TreeDto rootNode, String nodeName, String selectIds, List<String> selIds) {
		if (!StringUtil.isEmpty(selectIds)) {
			String[] selectId = selectIds.split(",");
			if (CommUtil.contains(selectId, node.getId())) {
				node.setChecked(true);
				this.setNodeOpen(node.getId(), rootNode);
			}
		}
		if (!StringUtil.isEmpty(nodeName) && node.getText().trim().contains(nodeName)) {
			// 找到查询的节点后，进行处理
			selIds.add(node.getId());
			// 遍历树，把查询后的节点对应的父节点全部展开
			this.setNodeOpen(node.getId(), rootNode);
		}
		// 遍历树的孩子节点
		List<TreeDto> childrens = this.getTreePoints(InitCacheData.getInstance().getCacheTreeData(InitCacheData.CacheType.WEB_TYPE), node.getId());
		node.setChildren(childrens);
		Iterator<TreeDto> nodeIt = childrens.iterator();
		while (nodeIt.hasNext()) {
			// 递归调用孩子中的每个节点
			this.getSelectMenuTree(nodeIt.next(), rootNode, nodeName, selectIds, selIds);
		}
	}
	
	/**
	 * @description 根据当前的节点逐层判断它的上级节点可是跟节点，如果不是，把该节点进行张开
	 * @author fengya
	 * @date 2015-7-30 下午04:45:13
	 * @param id
	 *            被选中的节点ID
	 * @param rootNode
	 *            根节点
	 * @return void
	 */
	private void setNodeOpen(String id, TreeDto rootNode) {
		if (id != null && !id.trim().equals(InitCacheData.ROOT)) {
			Map<String, Object> postMap = InitCacheData.getInstance().getCacheData(CacheType.WEB_TYPE);
			Object obj = postMap.get(id);
			if (obj != null) {
				WebTypeInfo wti = (WebTypeInfo) obj;
				// 得到树中对应ID的节点
				this.getNode(rootNode, wti.getPid());
				this.setNodeOpen(wti.getPid(), rootNode);
			}
		}
	}

	/**
	 * @description 展开根节点的ID
	 * @author fengya
	 * @date 2015-7-30 下午04:46:24
	 * @param node
	 * @param pid
	 * @return void
	 */
	private void getNode(TreeDto node, String pid) {
		if (node.getId().trim().equals(pid)) {
			node.setState(TreeDto.stateValue.OPEN.toString().toLowerCase());
		} else {
			Iterator<TreeDto> nodeIt = node.getChildren().iterator();
			while (nodeIt.hasNext()) {
				this.getNode(nodeIt.next(), pid);
			}
		}
	}
	
	/**
	 * @description 根据给点的节点ID，得到该节点下所有的孩子节点对象
	 * @author Ant-K
	 * @date 2016-03-16 19:50
	 * @param childrenDictionaryList
	 * @param id
	 * @return
	 * @return List<TreeDto>
	 */
	@SuppressWarnings("unchecked")
	private List<TreeDto> getTreePoints(Map<String, List> childrenArticleList, String id) {
		List<WebTypeInfo> childrenArticleType = childrenArticleList.get(id);
		List<TreeDto> treeList = new ArrayList<TreeDto>();
		if (childrenArticleType != null && !childrenArticleType.isEmpty()) {
			TreeDto treeDto = null;
			WebTypeInfo articManage = null;
			Iterator<WebTypeInfo> dictIt = childrenArticleType.iterator();
			while (dictIt.hasNext()) {
				articManage = dictIt.next();
				treeDto = new TreeDto();
				treeDto.setIconCls("icon-dict-icon");
				treeDto.setId(articManage.getId());
				treeDto.setText(articManage.getName());
				if (InitCacheData.getInstance().getCacheTreeData(InitCacheData.CacheType.WEB_TYPE).get(treeDto.getId()) != null) {
					treeDto.setChildren(new ArrayList<TreeDto>());
					treeDto.setState("closed");
				}
				treeList.add(treeDto);
			}
		}
		return treeList;
	}
	@Override
	public List<Dept> getDeptName(String deptId) {
		return 	this.webTypeManageDao.getDeptName(deptId);
	}
}
